package service.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Service;

import base.entity.Page;
import base.service.IBaseService;
import entity.file.FileName;
import entity.file.MyFile;
import entity.role.Role;
import entity.role.UserRole;
import entity.user.User;
import service.file.IFileService;
import service.role.IRoleService;
import util.MyConfig;

@Service("userServiceImpl")
public class UserServiceImpl implements IUserService {

	@Resource
	private IBaseService baseServiceImpl;
	@Resource
	private IFileService fileServiceImpl;
	@Resource
	private IRoleService roleServiceImpl;
	@Resource
	private MyConfig myConfig;

	@Override
	public User login(String account, String pwd) throws Exception {
		// TODO Auto-generated method stub
		User user = baseServiceImpl.findOne("from User where account = ? and password = ? and state = ?",
				new Object[] { account, pwd, true });
		return user;
	}

	@Override
	public User addSingleUser(User user) throws Exception {
		// TODO Auto-generated method stub
		baseServiceImpl.save(user);
		// 给用户增加最基本的角色
		Role role = baseServiceImpl.findOne("from Role where code = ?", new Object[] { myConfig.getBaseRoleCode() });
		UserRole userRole = new UserRole(role, user);
		List<UserRole> userRoles = new ArrayList<UserRole>();
		userRoles.add(userRole);
		roleServiceImpl.addUserToRole(userRoles);
		return user;
	}

	@Override
	public User UpdateUser(User user) throws Exception {
		// TODO Auto-generated method stub
		baseServiceImpl.saveOrUpdate(user);
		return user;
	}

	@Override
	public User updateUserAttachment(User user, String id, String name) throws Exception {
		// TODO Auto-generated method stub
		user = baseServiceImpl.getById(User.class, user.getId());
		FileName attachment = user.getAttachment();
		MyFile myFile = fileServiceImpl.getMyFileById(id);
		if (attachment != null) {
			fileServiceImpl.unUseFileName(attachment);
		}
		attachment = new FileName(name, myFile);
		fileServiceImpl.useFileName(attachment);
		user.setAttachment(attachment);
		baseServiceImpl.saveOrUpdate(user);
		return user;
	}

	@Override
	public User getUserById(String id) throws Exception {
		// TODO Auto-generated method stub
		return baseServiceImpl.getById(User.class, id);
	}

	@Override
	public Page<User> getUsersByPageAndSize(Integer pageNo, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
		Page<User> page = baseServiceImpl.findByCriteria(criteria, pageNo, pageSize);
		return page;
	}

	@Override
	public List<User> getUsersByIds(List<String> ids) throws Exception {
		// TODO Auto-generated method stub
		List<User> users = new ArrayList<>();
		if (ids.size() != 0) {
			users = baseServiceImpl.getByIdList(User.class, ids);
		}
		return users;
	}

}
